operator[]
public member function
reference operator[] (difference_type n) const;
Dereference iterator with offset
Returns a reference to the element located n elements ahead of the element pointed by the iterator.
The iterator must point to some object (must not be null pointing) in order to be dereferenciable.
Parameters
- n
- Number of elements to offset.
difference_type is a member type defined as an alias of the base iterator's own difference type (generally, a integral type).
Return value
A reference to the element pointed by the iterator offset by n elements forward.
reference is a member type defined as an alias of the base iterator's own reference type.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// reverse_iterator::operator[] example
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main () {
vector<int> myvector;
for (int i=0; i<10; i++) myvector.push_back(i); // myvector: 0 1 2 3 4 5 6 7 8 9
typedef vector<int>::iterator iter_int;
reverse_iterator<iter_int> rev_iterator = myvector.rbegin();
cout << "The fourth element from the end is : " << rev_iterator[3] << endl;
return 0;
}
|
Output:
The fourth element from the end is : 6
|
See also
operator* | Dereference iterator (public member function) |
operator-> | Dereference iterator (public member function) |
|